/**
 * @author rxliuli
 */
const app = (() => {
    let oldForm
    return new Vue({
        el: '#app',
        data() {
            return {
                activeName:'mode1',
                form: new SysRole({
                    status: 1,
                    includeSysUserList: [],
                    notIncludeSysUserList: [],
                    includeSysMenuList: [],
                    notIncludeSysMenuList: [],
                }),
                tab: 1,
                notIncludeSysUserSelectList: [],
                includeSysUserSelectList: [],
                includeSysMenuSelectList: [],
                notIncludeSysMenuSelectList: [],
            }
        },
        methods: {
            async deleteGroup() {
                const {arrayValidator: {isEmpty}} = rx
                if (!isEmpty(this.form.includeSysUserList)) {
                    await rxPrompt.warningMsg('当前权限组下仍有用户，无法删除')
                    return
                }
                if (await ajaxOption({
                    res: sysRoleApi.delete(this.form.id),
                    danger: '删除失败',
                    success: '删除成功',
                })) {
                    forward('system-config/admin_privilege')
                    sysRoleApi.listOfMap.clear()
                }
            },
            addGroup() {
                forward('/system-config/add/admin_privilege_group_add')
            },
            addSysUserList() {
                this.form.includeSysUserList.push(...this.notIncludeSysUserSelectList)
                this.form.notIncludeSysUserList = rx.filterItems(this.form.notIncludeSysUserList, this.notIncludeSysUserSelectList)
            },
            deleteSysUserList() {
                this.form.includeSysUserList = rx.filterItems(this.form.includeSysUserList, this.includeSysUserSelectList)
                this.form.notIncludeSysUserList.push(...this.includeSysUserSelectList)
            },
            addSysMenuList() {
                this.form.includeSysMenuList.push(...this.notIncludeSysMenuSelectList)
                this.form.notIncludeSysMenuList = rx.filterItems(this.form.notIncludeSysMenuList, this.notIncludeSysMenuSelectList)
            },
            deleteSysMenuList() {
                this.form.includeSysMenuList = rx.filterItems(this.form.includeSysMenuList, this.includeSysMenuSelectList)
                this.form.notIncludeSysMenuList.push(...this.includeSysMenuSelectList)
            },
            async updateSysRoleStatus({status}) {
                // TODO 这里需要后台 sys_role 表中添加状态字段
                if (await ajaxOption({
                    res: sysRoleApi.update({
                        id: this.form.id,
                        status,
                    }),
                    danger: '修改管理员组状态失败',
                    success: '修改管理员组状态成功',
                })) {
                    sysMenuApi.loadSysMenuList.clear()
                    sysRoleApi.listOfMap.clear()
                    forward("/system-config/admin_privilege");
                }
            },
            async updateSysRole() {
                const {id, name, description} = this.form
                if (await validate(this.form) && await ajaxOption({
                    res: sysRoleApi.update({id, name, description}),
                    danger: '修改管理员组信息失败',
                    success: '修改管理员组信息成功',
                })) {
                    sysMenuApi.loadSysMenuList.clear()
                    sysRoleApi.listOfMap.clear()
                }
            },
            async updateSysUser() {
                const {left, right} = rx.diffBy(this.form.includeSysUserList, oldForm.includeSysUserList, ({id}) => id)
                if (await ajaxOption({
                    res: sysRoleApi.updateUserRole(left.map(({id}) => ({
                        userId: id,
                        roleId: this.form.id,
                    })), right.map(({id}) => ({
                        userId: id,
                        roleId: this.form.id,
                    }))),
                    danger: '修改组成员失败，请稍后重试',
                    success: '修改组成员成功',
                })) {
                    oldForm.includeSysUserList = [...this.form.includeSysUserList]
                    oldForm.notIncludeSysUserList = [...this.form.notIncludeSysUserList]
                    sysMenuApi.loadSysMenuList.clear()
                }
            },
            async updateSysMenu() {
                const {left, right} = rx.diffBy(this.form.includeSysMenuList, oldForm.includeSysMenuList, ({id}) => id)
                if (await ajaxOption({
                    res: sysRoleApi.updateSysMenu(left.map(({id}) => ({
                        menuId: id,
                        roleId: this.form.id,
                    })), right.map(({id}) => ({
                        menuId: id,
                        roleId: this.form.id,
                    }))),
                    danger: '修改权限失败，请稍后重试',
                    success: '修改权限成功',
                })) {
                    oldForm.includeSysMenuList = [...this.form.includeSysMenuList]
                    oldForm.notIncludeSysMenuList = [...this.form.notIncludeSysMenuList]
                    sysMenuApi.loadSysMenuList.clear()
                }
            },
            getTabClass(tab) {
                return this.tab === tab ? 'ho' : ''
            },
            changeActiveTab(tab) {
                this.tab = tab
            },
            getTabShow(tab) {
                return this.tab === tab ? '' : 'hide'
            },
            dateFormat(date) {
                return rx.dateFormat(new Date(date), 'yyyy-MM-dd hh:mm:ss')
            },
        },
        computed: {},
        async mounted() {
            // noinspection ES6ModulesDependencies
            oldForm = _.merge(this.form, await sysRoleApi.get(forward.getData().id))
            this.form = _.cloneDeep(oldForm)
        },
    })
})()